<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/4/11
 * Time: 9:52
 */

namespace Admin\Controller;
use Admin\Api\MemberApi;
use Bbs\Api\BbsMemberApi;
use Bbs\Api\ForumSilentApi;
use Bbs\Api\CoreReportApi;
use Bbs\Api\ForumUserAdmingroupApi;
use Shop\Api\ProductApi;
use Bbs\Api\ForumThreadApi;
use Bbs\Api\ForumPostApi;

class BbsMemberController extends AdminController{

    /**
     * 禁言管理
     */
    public function banUser(){
        //判断是否有权限

        $result = apiCall(BbsMemberApi::GET_INFO,array(array('id'=>UID)));
        $allowbanuser = 0;
        if($result['status']){
            $allowbanuser = $result['info']['allowbanuser'];
            $this->assign('allowbanuser',$allowbanuser);
        }else{
            $this -> error($result['info']);
        }

        // if($allowbanuser){
        //     $fid = $result['info']['fid'];
        // }

        // if(!$allowbanuser){
        //     $this->display();
        //     exit();
        // }
        $allowpost = I('get.allowpost',1);
        $key       = urldecode(I('key','','trim'));
        $where = 'm.`status`=1';
        if($key) $where .= ' and (m.`nickname` like `%'.$key.'%` or m.`uid` like %'.$key.'%)  ';
        // if($allowpost == 0) $map['allowpost'] = 1;
        // $this->assign('allowpost',$allowpost);

        $page  = array('curpage' => I('get.p',0), 'size' => 15);
        // $order = 's.end_time desc';
        $params['key'] = $key;
        $field = "m.nickname,m.uid,count(m.uid) as count";
        $result = apiCall(ForumSilentApi::QUERY_SILENT,array($where,$page,false,$params,$field));
        // dump($result);exit;
        // $this->error($result);
        if ($result['status']) {
            //取得正确的结束时间
            $list = $result['info']['list'];
            foreach ($list as &$v) {
                $r = apiCall(ForumSilentApi::GET_INFO,array(array('uid'=>$v['uid']),'end_time desc'));
                if($r['status']){
                    $v['end_time'] = $r['info']['end_time'];
                }
            }
            // dump($list);exit;
            $this -> assign("show", $result['info']['show']);
            $this -> assign("list", $list);
        } else {
            $this -> error($result['info']);
        }

        $this->display();

    }

    /**
     * 禁言
     */
    public function ban(){
        if(IS_POST){
            $uid   = I('post.uid',0,'int');
            $time  = I('post.time',0);
            $r = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
            if(!$r['status']) $this->error('系统故障');
            if(empty($r['info'])) $this->error('无此用户');
            $r = apiCall(ForumUserAdmingroupApi::GET_INFO,array(array('uid'=>$uid)));
            if(!$r['status']) $this->error('系统故障');
            if(!empty($r['info'])) $this->error('不允许禁言管理');
            $r = apiCall(ForumSilentApi::GET_INFO,array(array('uid'=>$uid,'end_time'=>array('gt',NOW_TIME))));
            if(!$r['status']) $this->error('系统故障');
            $url = U('Admin/BbsMember/banUser');
            if(!empty($r['info'])){
                $id = $r['info']['id'];
                $entity = array(
                    'uid'=>$uid,
                    'start_time'=>NOW_TIME,
                    'end_time'=>strtotime($time),
                );
                $r = apiCall(ForumSilentApi::SAVE,array(array('id'=>$id),$entity));
                if(!$r['status']) $this->error('系统故障');
                $this->success('禁言覆盖成功',$url);
            }else{
                $entity = array(
                    'uid'=>$uid,
                    'start_time'=>NOW_TIME,
                    'end_time'=>strtotime($time),
                );
                $r = apiCall(ForumSilentApi::ADD,array($entity));
                if(!$r['status']) $this->error('系统故障');
                $this->success('禁言新增成功',$url);
            }

        }else{
            $uid   = I('get.uid',0,'int');
            if($uid){
                $r = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
                if($r['status']){
                    $this->assign('userinfo',$r['info']);
                }
            }
            $this->display();
        }
    }

    /**
     * 举报管理
     */
    public function coreReport(){
        
        $status    = I('status',-2);    //1审核 0正常 -1删除

        $operate = I('get.operate',0);
        $this->assign('operate',$operate);

        $page = array('curpage' =>  I('get.p',0), 'size' => 15);
        $order = 'id desc';

        if($operate==0){
            $map = array(
                'op_time' => 0
            );
        }else{
            $map['op_time'] = array('neq',0);
        }

        $params['operate'] = $operate;

        $result = apiCall(CoreReportApi::QUERY,array($map,$page,$order,$params));

//        dump($result);exit();

        if($result['status']){

            $this->assign('list',$result['info']['list']);
            $this->assign('show',$result['info']['show']);
        }else{
            $this->error($result['info']);
        }

//        $id=$result['info']['list']['id'];
//        dump($id);
//        $map='id='.$id;
//        $result1=apiCall(CoreReportApi::QUERY,array($map));
//        if ($result1['status']){
//            $t=$result1['info']['list']['0']['type'];
//            $gr=getReportType($t);
//            $this->assign('del',$gr);
//        }




        $this->display();

    }
    
    /**
     * 举报处理
     */
    public function operateCoreReport(){

        if(IS_POST){
            $id = I('get.id',-1);
            if(-1 == $id) $this->error('参数错误！');
            $op_time   = I('post.op_time',time());
            $op_result = I('post.op_result','');

            $result = apiCall(MemberApi::GET_INFO,array(array('uid'=>UID)));
            if($result['status']){
                $op_name = $result['info']['nickname'];
            }else{
                $this->error('异常错误！',U('Admin/BbsMember/coreReport'));
            }

            $entity = array(
                'op_time'   => strtotime($op_time),
                'op_result' => $op_result,
                'op_name'   =>$op_name
            );
            $result = apiCall(CoreReportApi::SAVE_BY_ID,array($id,$entity));

            if($result['status']){
                $this->success('操作成功!',U('Admin/BbsMember/coreReport'));
            }else{
                $this->error('操作失败!');
            }

        }else{
            $id = I('get.id',-1);
            if(-1 == $id){
                $this->error('参数错误！',U('Admin/BbsMember/coreReport'));
            }
            $this->display();
        }

    }
    //单个-1删除(默认)·0正常
    public function operatedelete(){
        $getid  = I('id');
        $map = "id=".$getid;



//        dump($id);exit();

        $result = apiCall(CoreReportApi::GET_INFO,array($map));
        if (!$result['status']) $this->error($result['info']);

        //2,判断举报类型
        $type = $result['info']['type'];
        $gr = getReportType($type);
        $tid = "tid=".$result['info']['report_id'];
        $id = "id=".$result['info']['report_id'];

        if ($gr=="帖子举报"){
            $entity['status'] = -1;
            $result = apiCall(ForumThreadApi::SAVE,array($tid,$entity));
            if($result['status']){
                //举报删除成功
                $this->successdelete($getid);
            }else{
                $this -> error($result['info']);
            }
        }

        if ($gr == '回复举报'){
            $entity['status'] = -1;
            $result = apiCall(ForumPostApi::SAVE,array($tid,$entity));
            if($result['status']){
               //举报删除成功
                $this->successdelete($getid);
            }else{
                $this -> error($result['info']);
            }
        }

        if ($gr == '赠无限举报'){
            $entity['status'] = -1;
            $result = apiCall(ProductApi::SAVE,array($id,$entity));
            if($result['status']){
                //举报删除成功
                $this->successdelete($getid);
            }else{
                $this -> error($result['info']);
            }
        }

        if ($gr == '未知举报'){
            $this -> error("未知举报");
        }
    }

    function successdelete($getid){

        $ent = array(
            'op_time'   => time(),
            'op_result' => '已删除',

        );
        $result = apiCall(CoreReportApi::SAVE_BY_ID,array($getid,$ent));
        if ($result['status']){
            $this -> success("操作成功!");
        }else{
            $this->error('操作失败!');
        }
    }
}